<?php

namespace App\Http\Controllers\MinApp;

use App\AmountModel;
use App\ArticleModel;
use App\HistoryModel;
use App\Http\Controllers\Controller;
use App\LikeModel;
use App\SeenModel;
use App\SliderModel;
use App\TypeModel;
use App\UserModel;
use App\VipModel;
use App\ZanModel;
use Illuminate\Http\Request;

class MinAppController extends Controller
{
    public function getslider(Request $request)
    {
        $sliders = SliderModel::get();
//        dd($sliders);
        return $sliders;
    }


    public function wxlogin(Request $request)
    {
        $openid = $request->input("openid");
        if ($openid) {
            $user = UserModel::where("openid", $openid)->first();
        } else {
            $code = $request->input("code");
            $userinfo = $request->input("userinfo");

            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . MINAPPID . "&secret=" . MINAPPSECRET . "&js_code=" . $code . "&grant_type=authorization_code";
            $result = json_decode(file_get_contents($url));
            $userinfo['openid'] = $result->openid;
            $userinfo['nickName'] = base64_encode($userinfo['nickName']);
            $user = UserModel::where("openid", $userinfo['openid'])->first();
            if ($user) {
                $user->update($userinfo);
            } else {
                UserModel::create($userinfo);
            }
        }
        $user['nickName'] = base64_decode($user['nickName']);
        return json_encode(['userInfo' => $user]);

    }


    public function getarticle(Request $request)
    {
        $articles = ArticleModel::get();
        return $articles;
    }


    public function gettypes(Request $request)
    {
        $types = TypeModel::get();
        return $types;
    }


    public function ar_detail(Request $request)
    {
        $id = $request->input("id");
        $articles = ArticleModel::where("id", $id)->first();
        return $articles;
    }


    public function orderby(Request $request)
    {
        $type_id = $request->input("type_id");
        $type_ar = ArticleModel::where("type", $type_id)->get();
        return $type_ar;
    }


    public function setseen(Request $request)
    {
        $ar_id = $request->input("ar_id");
        $openid = $request->input("openid");
        $seen = SeenModel::where("ar_id", $ar_id)->first();
        if (!$seen) {
            SeenModel::create($request->input());
        } else {
            $seen->update();
        }
    }


    public function getseen(Request $request)
    {
        $openid = $request->input("openid");
        $seens = SeenModel::leftjoin("article", "seen.ar_id", "article.id")->where("seen.openid", $openid)->get();
        return $seens;
    }


    public function setlike(Request $request)
    {
        $ar_id = $request->input("ar_id");
        $openid = $request->input("openid");
        $like = LikeModel::where("ar_id", $ar_id)->first();
        if (!$like) {
            LikeModel::create($request->input());
        } else {
            $like->update();
        }
    }


    public function getlike(Request $request)
    {
        $openid = $request->input("openid");
        $likes = LikeModel::leftjoin("article", "like.ar_id", "article.id")->where("like.openid", $openid)->get();
        return $likes;
    }


    public function setzan(Request $request)
    {
        $ar_id = $request->input("ar_id");
        $openid = $request->input("openid");
        $zan = ZanModel::where("ar_id", $ar_id)->first();
        if (!$zan) {
            ZanModel::create($request->input());
        } else {
            $zan->update();
        }
    }


    public function getzan(Request $request)
    {
        $openid = $request->input("openid");
        $zans = ZanModel::leftjoin("article", "zan.ar_id", "article.id")->where("zan.openid", $openid)->get();
        return $zans;
    }


    public function seencount(Request $request)
    {
        $openid = $request->input("openid");
        $seencount = SeenModel::where("openid", $openid)->count();
        return $seencount;
    }


    public function getamount(Request $request)
    {
        $openid = $request->input("openid");
        $amount = AmountModel::where("openid", $openid)->where("state", 1)->sum("amount");
        return $amount;
    }


    public function amounthistory(Request $request)
    {
        $openid = $request->input("openid");
        $history = AmountModel::where("amount.openid", $openid)->get();
        foreach ($history as $vh) {
            if ($vh["state"] == 1) {
                $vh["state"] = "成功";
            }
            else if ($vh["state"] == 0) {
                $vh["state"] = "失败";
            };
        }
        return $history;
    }


    public function viphistory(Request $request)
    {
        $openid = $request->input("openid");
        $viphistory = VipModel::where("vipuser.openid", $openid)->get();
        foreach ($viphistory as $vh) {
            if ($vh["paystate"] == 1) {
                $vh["paystate"] = "成功";
            }
            else if($vh["paystate"] == 0) {
                $vh["paystate"] = "失败";
            };
        }

        return $viphistory;
    }


    public function sreacharticle(Request $request)
    {
        $keyword = $request->input("keyword");
        $article = ArticleModel::where("title", "like", '%' . $keyword . '%')->orWhere("content", "like", '%' . $keyword . '%')->get()->toArray();
        return $this->success($article);
    }

    public function moveamount(Request $request)
    {
        $openid = $request->input("openid");
        $useramount = UserModel::where("openid", $openid)->first("amount");
        $amount=$useramount - 10;
        dd($amount);


        return;
    }
}
